package com.collect.discord.task;

import com.alibaba.fastjson.JSONArray;
import com.collect.common.eventbus.message.EventBusMessage;
import com.collect.common.eventbus.support.EventBusTemplate;
import com.collect.common.redis.RedisCache;
import com.collect.common.redis.RedisKeyUtils;
import com.collect.common.utils.eventbus.EventBusUtils;
import com.collect.common.utils.http.HttpClientUtils;
import com.collect.discord.jda.MessageEvent;
import com.collect.discord.web.domain.BinanceNewestPrice;
import com.collect.discord.web.domain.BinancePrice;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ Author :hky
 * @ Date  : Created in 11:12 2022-09-27
 * @ Version discord
 * @ Decribtion : 币安定时器$
 */

@Component
public class BinanceTask {
    @Resource
    private RedisCache redisCache;
    @Resource
    private EventBusTemplate eventBusTemplate;

    @Value("${binance.newestPriceUrl}")
    private String url;

    // 这个cron表达式代表从0小时开始，每过1小时执行一次。
    @Scheduled(cron = "* * 0/1 * * ?")
    public void getNewestPrice(){
        String result= HttpClientUtils.doGet(url, null,null,true);
        List<BinanceNewestPrice> list = JSONArray.parseArray(result, BinanceNewestPrice.class);
        list.forEach(e -> redisCache.setCacheMapValue(RedisKeyUtils.NEWESTPRICE,e.getSymbol(),e));
        //异步发送币安的价格消息
        eventBusTemplate.asyncPost(new EventBusMessage(EventBusUtils.BINANCEMESSAGE, null, list));
    }
}
